package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.DeviceSetting;
import com.wtwd.campus.entity.MqDeviceParam;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.DeviceSettingService;
import com.wtwd.campus.service.WarningRecordService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MsgType(value = Constants.DEVICE_PARAMETERS)
public class DeviceParameterReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private WarningRecordService warningRecordService;
    @Autowired
    private DeviceSettingService deviceSettingService;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqDeviceParam mqDeviceParam = (MqDeviceParam) mqMessage.getObject();
        String imei = mqDeviceParam.getImei();
        String commandNumber = mqDeviceParam.getCommandNumber();
        String ipPort = mqDeviceParam.getDefaultIpPort();
        String[] split = ipPort.split(",");
        String ip=split[0];
        Integer port=Integer.parseInt(split[1]);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            Long time = System.currentTimeMillis();
            Integer battery = mqDeviceParam.getBattery();
//            //若电量不足则存储低电量告警
//            if (battery < 20) {
//                //存储低电量告警
//                WarningRecord record = new WarningRecord();
//                record.setDevice_id(deviceInfo.getDeviceId());
//                record.setAddress(deviceInfo.getAddress());
//                record.setWarning_time(time);
//                record.setWarning_type(Constants.LOW_BATTERY_WARNING_TYPE);
//                warningRecordService.addWarningRecord(record);
//            }
            //修改设备信息
            Integer interval = mqDeviceParam.getInterval();
            String m1Card = mqDeviceParam.getM1Card();
            String firmware = mqDeviceParam.getFirmware();
            deviceInfo.setBattery(battery);
            deviceInfo.setLastUpTime(time);
            //修改设备设置信息
            DeviceSetting deviceSetting = deviceSettingService.getDeviceSettingByImei(imei);
            if(deviceSetting != null){
                deviceSetting.setIpAddress(ip);
                deviceSetting.setPort(port);
                deviceSettingService.modifyDeviceSetting(deviceSetting);
            }
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //删除缓存中的指令
            String delKey=Constants.DEVICE_PARAMETERS.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.DEVICE_PARAMETERS, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.DEVICE_PARAMETERS);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", imei);
            jsonObject.put("版本号", firmware);
            jsonObject.put("电量", battery);
            jsonObject.put("gps数", mqDeviceParam.getGpsNum());
            jsonObject.put("上报间隔", interval);
            jsonObject.put("设备时间", mqDeviceParam.getDeviceDate());
            jsonObject.put("设备当前信号值", mqDeviceParam.getCsq());
            jsonObject.put("ip 端口",ipPort);
            jsonObject.put("蓝牙功能开关",mqDeviceParam.getBtfLag());
            jsonObject.put("UPLOADFLAG",mqDeviceParam.getUploadFlag());
            jsonObject.put("tcp状态",mqDeviceParam.getTcpStatus());
            jsonObject.put("gps状态",mqDeviceParam.getGpsStatus());
            jsonObject.put("手机号",m1Card);
            jsonObject.put("macValid",mqDeviceParam.getMacValid());
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
